package org.ofbiz.restaurant;

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import javolution.util.FastList;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;

public class PrintServices {
    public static final String module = PrintServices.class.getName();
    public static final String resource = "RestaurantUiLabels";
    
    public static Map<String, Object> getPrinterList(DispatchContext ctx, Map<String, Object> context) {
        PrintService[] printerServiceArray = PrintServiceLookup.lookupPrintServices(null, null);
        List<String> printerList = FastList.newInstance();
        for (PrintService printerService : printerServiceArray) {
            printerList.add(printerService.getName());
        }
        Map<String, Object> result = ServiceUtil.returnSuccess();
        result.put("printerList", printerList);
        return result;
    }
    public static Map<String, Object> getDefaultPrinter(DispatchContext ctx, Map<String, Object> context) {
        PrintService printerService = PrintServiceLookup.lookupDefaultPrintService();
        String printerName = null;
        if (UtilValidate.isNotEmpty(printerService)) {
            printerName = printerService.getName();
        }
        Map<String, Object> result = ServiceUtil.returnSuccess();
        result.put("printerName", printerName);
        return result;
    }
    public static Map<String, Object> printPdfFile(DispatchContext ctx, Map<String, Object> context) {
        Locale locale = (Locale) context.get("locale");
        try {
            String filePath = (String) context.get("filePath");
            String printerName = (String) context.get("printerName");
            PDDocument pdd = PDDocument.load(filePath);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            PrintService[] printerServiceArray = PrintServiceLookup.lookupPrintServices(null, null);
            PrintService printerService = null;
            for (PrintService printerServiceEntry : printerServiceArray) {
                String printerNameEntry = printerServiceEntry.getName();
                if (UtilValidate.isNotEmpty(printerName)) {
                    if (printerName.equals(printerNameEntry)) {
                        printerService = printerServiceEntry;
                        break;
                    }
                }
            }
            if (printerService != null) {
	            printJob.setPrintService(printerService);
	            pdd.silentPrint(printJob);
            }
            //pdd.print(printJob);
        } catch (IOException e) {
            return ServiceUtil.returnError(e.getMessage());
        } catch (PrinterException e) {
            return ServiceUtil.returnError(e.getMessage());
        }
        String successMessage = UtilProperties.getMessage(resource, "RestaurantPrintSuccessful", locale);
        return ServiceUtil.returnSuccess(successMessage);
    }
}
